package com.example.eduservice.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.eduservice.client.OrderClient;
import com.example.eduservice.entity.EduCourse;
import com.example.eduservice.entity.EduTeacher;
import com.example.eduservice.entity.chapter.ChapterVo;
import com.example.eduservice.entity.frontvo.CourseFrontVo;
import com.example.eduservice.entity.frontvo.CourseWebVo;
import com.example.eduservice.service.EduChapterService;
import com.example.eduservice.service.EduCourseService;
import com.example.eduservice.service.EduTeacherService;
import com.example.utils.JwtUtils;
import com.example.utils.R;
import com.example.utils.ordervo.CourseWebVoOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


//@CrossOrigin
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourserFrontController {


    @Autowired
    private  EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //1.条件查询带分页

    @PostMapping("/getFrontCourseList/{page}/{limit}")
    public  R  getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                  @RequestBody(required = false) CourseFrontVo courseFrontVo){

            Page<EduCourse> coursePage=new Page<>(page,limit);

          Map<String,Object>  map=courseService.getFrontCourseList(coursePage,courseFrontVo);
            return R.ok().data(map);

    }

    //2  课程详情的方法
    @GetMapping("/getCourseInfo/{courseId}")
    public  R     getFrontCourseInfo(@PathVariable String courseId,HttpServletRequest request){

        //跟局课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByid(courseId);

        //根据课程di和用户id查询当前课程是否已经支付过了
        boolean buyCourse = orderClient.isBuyCourse(courseId,JwtUtils.getMemberIdByJwtToken(request));

        return  R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList).data("isBuy",buyCourse);





    }

    //根据课程id查询课程信息
    @PostMapping("/getCourseInfoOrser/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);

        CourseWebVoOrder courseWebVoOrder=new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);

        return  courseWebVoOrder;

    }





}
